﻿/**
 * nPnP - neither Perl not PHP
 *
 * Copyright (C) 2009 Christian Moeller
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; 
 * if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FParser.Executor;
using System.Threading;
using FParser.Runtime.Libraries.Threading;
using FParser.Parser;

namespace FParser.Runtime.Libraries
{
    public class Lib_String : IBaseLibrary
    {
        public Lib_String()
        { }

        public ExternalLibraryCall[] Exports
        {
            get
            {
                return new ExternalLibraryCall[] { 
                    trim,
                    str_get,
                    str_set,
                    ord,
                    chr,
                    str_concat
                };
            }
        }

        public ExecutionResult ord(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1))
            {
                char ordinary;
                if (char.TryParse(scope.GetParameter(1), out ordinary))
                {
                    result.sValue = Executor.Executor.GetAnonymousType(((int)ordinary).ToString());
                }
            }
            return result;
        }

        public ExecutionResult chr(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1))
            {
                int ordinary;
                if (int.TryParse(scope.GetParameter(1), out ordinary))
                {
                    result.sValue = Executor.Executor.GetAnonymousType(((char)ordinary).ToString());
                }
            }
            return result;
        }

        public ExecutionResult trim(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1))
            {
                string str = scope.GetParameter(1);
                result.State.Result = EnumExecutionResult.OK;
                result.sValue = BaseType.CreateAnonymous(str.Trim());
            }
            return result;
        }

        public ExecutionResult str_get(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1) && scope.HasParameter(2))
            {
                string p_Data = scope.GetParameter(1);
                string p_Index = scope.GetParameter(2);
                int index = int.Parse(p_Index);

                result.sValue = Executor.Executor.GetAnonymousType(p_Data[index].ToString());
            }
            return result;
        }

        public ExecutionResult str_set(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1) && scope.HasParameter(2) && scope.HasParameter(3))
            {
                string res_String = scope.GetParameter(1);
                int ordinary;
                if (int.TryParse(scope.GetParameter(2), out ordinary))
                {
                    char p_Char = (char)ordinary;
                    string p_Index = scope.GetParameter(3);
                    int index = int.Parse(p_Index);
                    char[] p_String = res_String.ToCharArray();
                    p_String[index] = p_Char;
                    res_String = new String(p_String);
                    result.sValue = Executor.Executor.GetAnonymousType(res_String);

                }
            }
            return result;
        }

        public ExecutionResult str_concat(RuntimeScope scope)
        {
            ExecutionResult result = new ExecutionResult();
            if (scope.HasParameter(1) && scope.HasParameter(2))
            {
                string data1 = scope.GetParameter(1);
                string data2 = scope.GetParameter(2);
                result.sValue = Executor.Executor.GetAnonymousType(data1 + data2);
            }
            return result;
        }


    }
}
